//
//  TMGXLineModel.m
//  TMGold
//
//  Created by Mac on 2018/3/19.
//  Copyright © 2018年 tangmu. All rights reserved.
//

#import "TMGXLineModel.h"
#import "TMGGoldHistoryModel.h"
@interface TMGXLineModel()
@property(nonatomic, strong) NSArray                     *dataSouce;
@property(nonatomic, strong) NSMutableArray                     *values;
@property(nonatomic, strong) TMGGoldHistoryModel * model1;
@property(nonatomic, strong) TMGGoldHistoryModel * model2;
@property(nonatomic, strong) TMGGoldHistoryModel * model3;
@property(nonatomic, strong) TMGGoldHistoryModel * model4;
@property(nonatomic, strong) TMGGoldHistoryModel * model5;
@end
@implementation TMGXLineModel
- (instancetype)initWithDataSouce:(NSArray *)dataSouce{
    if (self == [super init]) {
        self.dataSouce = dataSouce;
        self.model1 = [self modelFormString:@"00"];
        self.model2 = [self modelFormString:@"06"];
        self.model3 = [self modelFormString:@"12"];
        self.model4 = [self modelFormString:@"18"];
        self.model5 = [self modelFormString:@"24"];
        self.values = [NSMutableArray new];
    }
    return self;
}
-(NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis
{
    NSInteger index = value   ;
    if (![self.values containsObject:@(value)]) {
        [self.values addObject:@(value)];
        NSInteger indextest = [self.values indexOfObject:@(value)];

            if (indextest == 0) {
                return @"00:00";
            }
            if (indextest == 1) {
                return @"06:00";
            }
            if (indextest == 2) {
                return @"12:00";
            }
            if (indextest == 3) {
                return @"18:00";
            }
            if (indextest == 4) {
                return @"24:00";
            }
        
    }else{
        NSInteger indextest = [self.values indexOfObject:@(value)];

            if (indextest == 0) {
                return @"00:00";
            }
            if (indextest == 1) {
                return @"06:00";
            }
            if (indextest == 2) {
                return @"12:00";
            }
            if (indextest == 3) {
                return @"18:00";
            }
            if (indextest == 4) {
                return @"24:00";
            }
        
    }
    
    TMGGoldHistoryModel * model;
    
    if (index == 0) {
        return @"00:00";
    }
    if (index == 24) {
        return @"06:00";
    }
    if (index == 60) {
        return @"12:00";
    }
    if (index == 90) {
        return @"18:00";
    }
    if (index == 120) {
        return @"24:00";
    }
    if (index >0 && index < self.dataSouce.count) {
        model =  self.dataSouce[index - 1];
        if ([model.create_at isEqualToString:self.model1.create_at] ) {
            return @"00:00";
        }
        if ([model.create_at isEqualToString:self.model2.create_at] ) {
            return @"06:00";
        }
        if ([model.create_at isEqualToString:self.model3.create_at] ) {
            return @"12:00";
        }
        if ([model.create_at isEqualToString:self.model4.create_at] ) {
            return @"18:00";
        }
        if ([model.create_at isEqualToString:self.model5.create_at] ) {
            return @"24:00";
        }
    }

    return @"";

    NSArray * myarray = @[@"06:00",@"12:00",@"18:00",@"24:00"];
    NSString * name = [model.create_at componentsSeparatedByString:@" "].lastObject;
    NSArray * array = [name componentsSeparatedByString:@":"];
    name = [NSString stringWithFormat:@"%@:%@",array.firstObject,array[1]];
//    for (NSString * text in myarray) {
//        if ([name containsString:text]) {
//             return name;
//        }
//    }
//    if ([myarray containsObject:name]) {
//        return name;
//    }
//    return @"";
     return name;
}
- (TMGGoldHistoryModel*)modelFormString:(NSString*)string{
    for (TMGGoldHistoryModel *model in self.dataSouce) {
        if ([model.create_at containsString:string]) {
            return model;
        }
    }
    return nil;
}
@end
